<template>
  <div class="layui-row nav-top">
    <div class="layui-container">
      <ul class="layui-nav x_nav">
        <li class="layui-nav-item" v-for="(item, index) in items" :key="index">
          <a
            :href="item.href"
            :class="[item.isActive ? 'active' : 'tip']"
            @click="toggle(item)"
            >{{ item.name }}</a
          >
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  props: ["activeId"],
  data() {
    return {
      items: [
        { href: "/", name: "首页", isActive: false },
        { href: "/question", name: "有问必答", isActive: false },
        { href: "/class", name: "班级", isActive: false },
        { href: "", name: "开卷有益", isActive: false },
        { href: "", name: "积分排行", isActive: false },
      ],
    };
  },
  methods: {
    toggle(item) {
      this.items.forEach((d) => {
        d.isActive = false;
      });
      item.isActive = true;
    },
  },
  watch: {
    activeId: {
      handler: function(val, oldVal) {
        this.items[val].isActive = true;
      },
      immediate: true
    }
  }
};
</script>

<style>
.nav-top {
  background-color: #fff;
  box-shadow: 0 1px 5px 0 #eee;
  margin-bottom: 0;
}

body .x_nav {
  background-color: transparent !important;
}

body .x_nav .layui-nav-item > a {
  color: #074b60;
  /* padding: 0 30px; */
  line-height: 3rem;
  display: block;
  position: relative;
  cursor: pointer;
}

body .x_nav .layui-nav-item > a.tip:hover {
  color: #5fb878;
}

body .x_nav .layui-nav-item > a.tip:hover::before {
  transform: scaleX(5);
}

body .x_nav .layui-nav-item > a.tip::before {
  content: "";
  position: absolute;
  bottom: 0;
  left: calc(50% - 0.5rem);
  height: 2px;
  width: 1rem;
  transform: scaleX(0);
  transition-property: all;
  transition-duration: 0.2s;
  background-color: #5fb878;
}

body .x_nav .layui-nav-item > a.active {
  color: #5fb878;
  transform: scale(1.25);
}
</style>